package com.tangtang.gulimall.product.service.impl;

import com.tangtang.gulimall.product.dao.BrandDao;
import com.tangtang.gulimall.product.dao.CategoryDao;
import com.tangtang.gulimall.product.entity.BrandEntity;
import com.tangtang.gulimall.product.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangtang.common.utils.PageUtils;
import com.tangtang.common.utils.Query;

import com.tangtang.gulimall.product.dao.CategoryBrandRelationDao;
import com.tangtang.gulimall.product.entity.CategoryBrandRelationEntity;
import com.tangtang.gulimall.product.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    //商品
    @Resource
    private CategoryDao categoryDao;

    //品牌
    @Resource
    private BrandDao brandDao;

    //关联分类表的保存
    @Resource
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveall(CategoryBrandRelationEntity categoryBrandRelationEntity) {
        //根据传来的品牌id和分类id将各种的名称进行查询
        Long brandId = categoryBrandRelationEntity.getBrandId();
        Long categoryId = categoryBrandRelationEntity.getCatelogId();

        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(brandId);
        System.out.println(brandEntity.getName());
        System.out.println(categoryEntity.getName());

        categoryBrandRelationEntity.setBrandName(brandEntity.getName());
        categoryBrandRelationEntity.setCatelogName(categoryEntity.getName());

        //直接插入一条数据
        //categoryBrandRelationDao.insert(categoryBrandRelationEntity);
        //跟是使用insert一样。但是性能可以好点
        this.save(categoryBrandRelationEntity);
    }

    @Override
    public List<BrandEntity> getbrandid(Long catId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = this.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId)
        );
        //将查询出来的id放入一个集合
        List<BrandEntity> brandEntities = new ArrayList<BrandEntity>();
        if(categoryBrandRelationEntities!=null)
        {
            for (CategoryBrandRelationEntity categoryBrandRelationEntity : categoryBrandRelationEntities) {
                BrandEntity brandEntity = brandDao.selectById(categoryBrandRelationEntity.getBrandId());
                brandEntities.add(brandEntity);
            }
        }
        return brandEntities;
    }
}